Skip to content

Conversation

@rajshivu
Copy link

@rajshivu rajshivu commented Feb 9, 2026

Updated CreateASTVisitor to map refinement variables '_' and 'return' to '$result' to prevent conflicts.
Verified by manual test in ReturnRefinementTest.java.
firstos

Copy link
Collaborator

@rcosta358 rcosta358 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rajshivu,
This is not really what we were looking for. Your approach replaces the variables return and _ with $result, which is incorrect and causes the tests to fail.

To allow $result to represent return values of methods, you need to modify the grammar to allow identifiers to start with $, substitute the variable $result with _ (similarly to return), and finally add some tests to check if your implementation is correct.

Copy link
Collaborator

@rcosta358 rcosta358 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you have any suggestions other than $result, which is not very idiomatic for Java, feel free to let us know!

@rajshivu
Copy link
Author

Hi @rcosta358 ,
Thank you for the clarification - that makes sense. You’re right, my initial approach was too naive and incorrectly replaced return and _ directly with $result, which explains the failing tests.

Based on your feedback, I understand the correct direction is to:

  1. Extend the grammar to allow identifiers starting with $.
  2. Treat $result as a syntactic alias and internally substitute it with _ (similarly to how return is handled today).
  3. Add proper tests to validate that $result, _, and return behave consistently.

I’ll update the implementation accordingly and push a revised PR.
Also, I agree that $result may not be the most idiomatic Java-style identifier — I’ll think about alternative suggestions and share them if I find a better fit.

Thanks again for the guidance, this was very helpful.

@rajshivu
Copy link
Author

Fixed the handling of method return values in refinements:
Updated the grammar and AST visitor to allow $result to represent method return values, while still keeping _ as a valid alias.

Added a regression test ResultRefinementRegression.java to verify $result works correctly in refinements.
Ensured existing tests in TestMultiple.java pass with the new $result handling.

Hope This change resolves the issue where return was incorrectly treated as a variable and ensures $result can be safely used in refinement expressions.

Copy link
Collaborator

@rcosta358 rcosta358 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rajshivu,
Almost there. Your implementation is not quite correct yet. Currently what you're doing is replacing all return and $result variables with _ in the CreateASTVisitor. That way, the following code passes, when it shouldn't:

void test() {
  @Refinement("$result > 0") // $result is replaced with `_`, which refers to `x`, so it passes
  int x = 1;
}

Actually, $result or return should only be allowed on refinements that target methods, such as:

@Refinement("$result > 0")
int test() {
  return 1;
}

So, we don't actually want to modify the CreateASTVisitor. Instead, we want to modify the MethodsFunctionsChecker. More specifically, the handleFunctionRefinements, and add an additional case for $result, similarly to return:

private Predicate handleFunctionRefinements(RefinedFunction f, CtElement method, List<CtParameter<?>> params) throws LJError {
  // ...
  ret = ret.substituteVariable("return", Keys.WILDCARD);
  ret = ret.substituteVariable("$result", Keys.WILDCARD); // ADD THIS
  f.setRefReturn(ret);
  // ...
}

Finally, please add tests to liquidjava-example/src/main/java/testSuite, not liquidjava-example/src/main/java/testMultiple. For tests that should pass, they must include "Correct" in their file name, whereas for tests that should fail, they should include "Error", along with the expected error in the first line of the file as a comment. To run the tests locally, you can run mvn test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't make changes to this file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also not modify this, since you should include your tests in testSuite and not testMultiple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants